import { router } from "@/router";
import type { App } from "vue";
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        // 用于 keep-alive 功能，需要与 SFC 中自动推导或显式声明的组件名称一致
        // 参考文档: https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        name: "Dashboard",
        meta: {
          title: "dashboard",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
      {
        path: "profile",
        name: "Profile",
        component: () => import("@/views/profile/index.vue"),
        meta: { title: "个人中心", icon: "user", hidden: true },
      },
      {
        path: "shopInfo",
        name: "ShopInfo",
        component: () => import("@/views/orderManagement/shopInfo.vue"),
        meta: { hidden: true, title: "店铺信息", keepAlive: true },
      },
    ],
  },
  // 外部链接
  // {
  //   path: "/external-link",
  //   component: Layout,
  //   children: [{
  //     component: () => import("@/views/external-link/index.vue"),
  //     path: "https://www.cnblogs.com/haoxianrui/",
  //     meta: { title: "外部链接", icon: "link" },
  //   },
  //   ],
  // },
  // 多级嵌套路由

  {
    path: "/order",
    component: Layout,
    name: "/Order",
    redirect: "/order/order",
    meta: {
      title: "订单",
      icon: "order",
    },
    children: [
      {
        path: "order",
        component: () => import("@/views/orderManagement/index.vue"),
        name: "Order",
        meta: {
          title: "订单管理",
          icon: "order",
          keepAlive: true,
        },
      },
      {
        path: "logistics",
        component: () =>
          import("@/views/orderManagement/Logistics-delivery.vue"),
        name: "Logistics",
        meta: {
          title: "物流发货",
          icon: "order",
          keepAlive: true,
        },
      },
      {
        path: "faceSheet",
        component: () => import("@/views/orderManagement/faceSheet.vue"),
        name: "FaceSheet",
        meta: {
          title: "电子面单",
          icon: "order",
          keepAlive: true,
        },
      },
      {
        path: "detail/:id",
        component: () => import("@/views/orderManagement/detail.vue"),
        props: true,
        name: "Detail",
        meta: {
          hidden: true,
          title: "订单详情",
        },
      },
    ],
  },

  {
    path: "/shopgoods",
    name: "/Shopgoods",
    component: Layout,
    redirect: "/shopgoods",
    meta: {
      title: "商品",
      icon: "goods",
    },
    children: [
      {
        path: "shopgoods",
        component: () => import("@/views/shopgoods/index.vue"),
        name: "shopgoods",
        meta: {
          title: "商品管理",
          icon: "none",
        },
      },
      {
        path: "shopgoods",
        component: () => import("@/views/shopgoods/index.vue"),
        name: "shopgoods",
        meta: {
          title: "发布商品",
          icon: "none",
        },
      },
    ],
  },

  {
    path: "/message",
    name: "/Message",
    component: Layout,
    redirect: "/message",
    children: [
      {
        path: "message",
        component: () => import("@/views/message/index.vue"),
        name: "Message",
        meta: {
          title: "消息管理",
          icon: "msg",
        },
      },
    ],
  },
  {
    path: "/contentManagement",
    name: "/ContentManagement",
    component: Layout,
    redirect: "/contentManagement",
    children: [
      {
        path: "contentManagement",
        component: () => import("@/views/contentManagement/index.vue"),
        name: "ContentManagement",
        meta: {
          title: "内容管理",
          icon: "count",
        },
      },
    ],
  },
  {
    path: "/dataAnalysis",
    name: "/DataAnalysis",
    component: Layout,
    meta: {
      title: "数据分析",
      icon: "Chart-line",
    },
    children: [
      {
        path: "dataAnalysis",
        component: () => import("@/views/dataAnalysis/index.vue"),
        name: "DataAnalysis",
        redirect: "/dataAnalysis/dataAnalysis/lave1",
        meta: {
          title: "数据分析1",
          icon: "Chart-line",
        },
        children: [
          {
            path: "lave1",
            component: () => import("@/views/lave1/index.vue"),
            name: "Lave1",
            meta: {
              title: "123",
              icon: "Chart-line",
            },
          },
          {
            path: "lave2",
            component: () => import("@/views/lave2/index.vue"),
            name: "Lave2",
            meta: {
              title: "234",
              icon: "Chart-line",
              hidden: true,
            },
          },
        ],
      },
      {
        path: "lave1",
        component: () => import("@/views/lave1/index.vue"),
        name: "Lave1",
        meta: {
          title: "123",
          icon: "Chart-line",
        },
      },
    ],
  },
  {
    path: "/assetManagement",
    name: "/AssetManagement",
    component: Layout,
    redirect: "/assetManagement",
    children: [
      {
        path: "assetManagement",
        component: () => import("@/views/assetManagement/index.vue"),
        name: "AssetManagement",
        meta: {
          title: "资产管理",
          icon: "position",
        },
      },
    ],
  },

  {
    path: "/nested",
    component: Layout,
    redirect: "/nested/level1",
    name: "Nested",
    meta: { title: "营销管理", icon: "maslow" },
    children: [
      {
        path: "coupon",
        component: () => import("@/views/nested/coupon/coupon.vue"),
        name: "Coupon",
        meta: { title: "优惠券", icon: "coupon" },
      },
      {
        path: "timeDiscount",
        component: () => import("@/views/nested/timeDiscount/timeDiscount.vue"),
        name: "TimeDiscount",
        meta: { title: "限时折扣", icon: "coupon" },
      },
      {
        path: "maxout",
        component: () => import("@/views/nested/maxout/maxout.vue"),
        name: "Maxout",
        meta: { title: "满减满折", icon: "coupon" },
      },
      {
        path: "groupBooking",
        component: () => import("@/views/nested/groupBooking/groupBooking.vue"),
        name: "GroupBooking",
        meta: { title: "拼团", icon: "coupon" },
      },
      {
        path: "setMeal",
        component: () => import("@/views/nested/setMeal/setMeal.vue"),
        name: "SetMeal",
        meta: { title: "套餐", icon: "coupon" },
      },
      {
        path: "sceneMarketing",
        component: () =>
          import("@/views/nested/sceneMarketing/sceneMarketing.vue"),
        name: "SceneMarketing",
        meta: { title: "场景营销", icon: "coupon" },
      },
    ],
  },
  {
    path: "/settings",
    name: "/Settings",
    component: Layout,
    redirect: "/settings",
    children: [
      {
        path: "settings",
        component: () => import("@/views/settings-page/index.vue"),
        name: "Settings",
        meta: {
          title: "设置",
          icon: "set",
        },
      },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: "/login" });
}

export default router;
